﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;


namespace PlayNote
{
    public static class MyExtensions
    {
        /// <summary>
        /// 显示提示消息
        /// </summary>
        /// <param name="win"></param>
        /// <param name="msg"></param>
        public static void ShowMsg(this Window win, string msg)
        {

            uint MONITOR_DEFAULTTONEAREST = 2;
            // 获取当前窗口的句柄
            IntPtr windowHandle = new WindowInteropHelper(win).Handle;

            // 获取当前窗口所在的监视器
            IntPtr hMonitor = Win32Helper.MonitorFromWindow(windowHandle, MONITOR_DEFAULTTONEAREST);
            if (hMonitor == IntPtr.Zero)
            {
                MessageBox.Show("无法获取监视器句柄");
                return;
            }

            Win32Helper.MONITORINFO monitorInfo = new Win32Helper.MONITORINFO();
            monitorInfo.cbSize = Marshal.SizeOf(typeof(Win32Helper.MONITORINFO));
            if (!Win32Helper.GetMonitorInfo(hMonitor, ref monitorInfo))
            {
                MessageBox.Show($"无法获取监视器信息，错误代码：{Marshal.GetLastWin32Error()}");
                return;
            }

            // 获取当前监视器的工作区信息
            Win32Helper.RECT workArea = monitorInfo.rcWork;

            TipWin tipWin = new TipWin();
            tipWin.LayoutUpdated += (s, e) =>
            {
                tipWin.Left = workArea.left + (workArea.right - workArea.left - tipWin.Width) / 2;
                tipWin.Top = workArea.top;
            };
            tipWin.txt.Text = msg;
            tipWin.Show();
        }
    }
}
